package com.hoyo.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.hoyo.domain.Comment;
import com.hoyo.domain.CommentReport;
import com.hoyo.domain.ReportItem;

@Mapper
public interface CommentMapper {
	
	@Insert("insert into comment(movie_id, user_id, comment) values(#{movieId}, #{userId}, #{comment})")
	void save(Comment cmt);
	
	@Insert("insert into comment_report(movie_id, user_id, comment_id, reason,type) values(#{movieId}, #{userId}, #{commentId}, #{reason},#{type})")
	void saveCommentReport(CommentReport cmtr);
	
	@Select("insert into re_comment(user_id, comment_id, comment, reply_user_id, reply_user_nickname) values(#{userId}, #{commentId},"
			+ "#{comment}, #{replyUserId}, #{replyUserNickname})")
	void saveReplyComment(Comment comment);
	
	@Select("select c.*, mci.comment_amount total, ui.nickname, ui.icon from comment c left join user_info ui on ui.user_id=c.user_id "
			+ "left join movie_count_info mci on mci.movie_id = c.movie_id "
			+ "where c.movie_id=#{0} order by c.create_time desc limit #{1},#{2}")
	List<Comment> findCommentsByMovieIdOrderByCreateTimeDescAndLimitBy(int movieId, int offset, int count)throws Exception;
	
	@Select("select rc.*, ui.nickname, ui.icon,rc.reply_user_nickname, (select count(*) from re_comment where comment_id = #{0}) total "
			+"from re_comment rc left join user_info ui on ui.user_id=rc.user_id "
			+"where rc.comment_id = #{0} order by rc.like_points desc limit #{1},#{2}")
	List<Comment> findReplyCommentsByCommentIdOrderByLikPointsDescAndLimitBy(int commentId , int offset, int count)throws Exception;
	
	@Select("select c.*, mci.comment_amount total, ui.nickname, ui.icon from comment c left join user_info ui on ui.user_id=c.user_id "
			+ "left join movie_count_info mci on mci.movie_id = c.movie_id "
			+ "where c.movie_id=#{0} order by c.like_points desc limit #{1},#{2}")
	List<Comment> findCommentsByMovieIdOrderByLikePosintsDescLimitBy(int movieId, int offset, int count) throws Exception;
	
	@Select("select * from comment where id=#{commentId}")
	Comment findOneById(int commentId)throws Exception;
	
	@Select("select cmtr.id, cmt.comment content, cmtr.reason description, ui1.user_id, ui1.nickname user_nickname, ui2.user_id be_reported_user_id, "
			+ "ui2.nickname be_reported_user_nickname, (select count(*) from comment_report where is_solved =#{0}) total "
			+ "from comment_report cmtr left join user_info ui1 "
			+ "on ui1.user_id = cmtr.user_id left join comment cmt on cmt.id=cmtr.comment_id left join user_info ui2 "
			+ "on ui2.user_id = cmt.user_id where cmtr.is_solved =#{0} order by cmtr.create_time desc limit #{1},#{2}")
	List<ReportItem> findBeReportCommentsByCheckOrderByCreateTimeDescLimitBy(boolean checked, int offset, int count)throws Exception;
	
	@Update("update comment_report set is_solved=#{1}, is_really=#{2} where id=#{0}")
	void updateIsSolvedAndIsReallyById(int id, boolean isSolved, boolean isReally) throws Exception;
	
	@Select("select c.*, ui.icon, ui.nickname, uc.catalog_name, uc.catalog_poster, "
			+ "(select count(*) from comment) total "
			+ "from comment c left join user_info ui on ui.user_id=c.user_id "
			+ "left join movie mv on mv.id=c.movie_id left join user_catalog uc on uc.id=mv.catalog_id "
			+ "limit #{0},#{1}")
	List<Comment> findCommentsLimitBy(int offset, int count);
	
	@Update("delete from comment where id=#{0}")
	void deleteOneById(int id) throws Exception;
	
	@Update("select comment_id from comment_report where id=#{0}")
	int findCommentIdByReportId(int id) throws Exception;
	
	@Select("select count(*) from re_comment where comment_id=#{0}")
	int selectCountFromReCommentByCommentId(int commentId)throws Exception;
	
	@Update("delete from re_comment where comment_id=#{0}")
	int delteReplyCommentsByCommentId(int commentId);
}












